class PointsController < ApplicationController
  def index
    points = 0
    users = User.all(un: params[:u])
    points = users[0].pt unless users.length == 0 or users[0].pt < 0
    respond_to do |format|
      format.json { render json: points }
    end
  end

  def show
    return unless params[:id] == 'rank'

    pointsrank = {}
    users = User.all user_lang: { :'$elemMatch' => { la: params[:l], cl: params[:c] } }
    users.each do |user|
      pointsrank[user.un] = user.pt || 0
    end
    json = pointsrank.sort_by { |user, rank| rank }
    json.reverse!
    user_rank = 1
    requested_user = params[:u]
    json.each do |value|
      if value[0] == requested_user
        break
      else
        user_rank += 1
      end
    end
    json << user_rank
    respond_to do |format|
      format.json { render json: json }
    end
  end
end
